using System;
using System.Collections.Generic;
using System.Reflection;

namespace kbd.library.helper
{
    public static class logger
    {

        #region Color
        #endregion
        private static string GetColor(LogLevels level)
        {
            string color = "White";
            switch (level)
            {
                case LogLevels.Critical:
                    color = "Red";
                    break;
                case LogLevels.High:
                    color = "Blue";
                    break;
                case LogLevels.Information:
                    color = "Gray";
                    break;
                default:
                    color = "White";
                    break;
            }
            return color;
        }

        private static void SetColor(LogLevels level)
        {
            Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), GetColor( level));
        }

        private static void ResetColor()
        {
            Console.ForegroundColor = ConsoleColor.Gray;
        }

        public static void Log(LogLevels level, string title, string detail = null, Exception ex = null)
        {
            SetColor(level);
            Console.WriteLine("{0} {1} {2} ", title, detail ?? "", (ex == null) ? "" : ex.Message);
            ResetColor();
        }

        public static void PrintDetail<T>(object o)
        {
            Type t = o.GetType();
            Console.WriteLine("[{0}]", t.FullName);
            PropertyInfo[] props = t.GetProperties();
            foreach (PropertyInfo prp in props)
            {
                object value = prp.GetValue(o, new object[] { });
                Console.Write("\t {0}: {1}", prp.Name, value);
            }
            Console.WriteLine("");
        }
    }
}
